package bookstore.controller;

import bookstore.handle.Msg;
import bookstore.mapper.ShopCartMapper;
import bookstore.model.ShopCart;
import bookstore.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@CrossOrigin
@RestController
public class CarController {
    @Autowired
    ShopCartMapper shopCartMapper;

    @Autowired
    ShopCartService shopCartService;

    //得到用户所有的购物车列表
    @RequestMapping("/selectAllCarts")
    public Object select(Integer userId) {
        List<ShopCart> list = shopCartMapper.selectByUserid(userId);
        Collections.sort(list, new Comparator<ShopCart>() {
            @Override
            public int compare(ShopCart o1, ShopCart o2) {
                return o2.getId()-o1.getId();
            }
        });
        return new Msg(200, "", list);
    }

    //添加购物车
    @RequestMapping("/addOneBookInCart")
    public Object addOneBookInCart(Integer userId, Integer bookId, Integer count) {
        return shopCartService.addOneBookInCart(userId, bookId, count);
    }

    //删除购物车
    @RequestMapping("/delOneCart")
    public Object delOneCart(Integer userId, String listBookId) {
        return shopCartService.delOneCart(userId, listBookId);
    }

    //购物车数量+1/-1  1：+1  2：-1
    @RequestMapping("addCartCount")
    public Object addCartCount(Integer userId, Integer type, Integer bookId) {
        return shopCartService.addCartCount(userId, type, bookId);

    }
}
